package org.agony2d.gpu.core {
	import flash.geom.Point;
	
	import org.agony2d.core.agony_internal;
	import org.agony2d.gpu.supportClasses.buffers.VertexCombine;
	import org.agony2d.gpu.supportClasses.textures.TextureProxy;
	import org.agony2d.input.Touch;
	
	use namespace agony_internal;
	
public class PixelsAA extends NodeAA {
  
    public function PixelsAA() {
		m_combine = VertexCombine.NewVertexCombine()
    }
	
	public function get width() : int {
		if (!m_renderTex) {
			return 0
		}
		return m_renderTex.m_frame ? m_renderTex.m_frame.width : m_renderTex.m_realityWidth
	}
	
	public function get height() : int {
		if (!m_renderTex) {
			return 0
		}
		return m_renderTex.m_frame ? m_renderTex.m_frame.height : m_renderTex.m_realityHeight
	}
	
	override public function globalToLocal( globalX:Number, globalY:Number, result:Point = null ) : Point {
		if (!result) {
			result = new Point
		}
		this.doUpdateUpward()
		cachedMatrix.invert()
		result.setTo(cachedMatrix.a * globalX + cachedMatrix.c * globalY + cachedMatrix.tx, cachedMatrix.d * globalY + cachedMatrix.b * globalX + cachedMatrix.ty)
		if (m_renderTex.m_frame) {
			result.offset( - m_renderTex.m_frame.x - m_pivotX, - m_renderTex.m_frame.y - m_pivotY)
		}
		else if (m_pivotX != 0 || m_pivotY != 0) {
			result.offset( - m_pivotX, - m_pivotY)
		}
		return result
	}
	
	override public function localToGlobal( localX:Number, localY:Number, result:Point = null ) : Point {
		if (!result) {
			result = new Point
		}
		this.doUpdateUpward()
		localX += (m_renderTex && m_renderTex.m_frame) ? m_pivotX + m_renderTex.m_frame.x : m_pivotX
		localY += (m_renderTex && m_renderTex.m_frame) ? m_pivotY + m_renderTex.m_frame.y : m_pivotY
		result.setTo(cachedMatrix.a * localX + cachedMatrix.c * localY + cachedMatrix.tx, cachedMatrix.d * localY + cachedMatrix.b * localX + cachedMatrix.ty)
		return result
	}
	
	
	
	agony_internal var m_combine:VertexCombine
	agony_internal var m_renderTex:TextureProxy
	agony_internal var m_bindingTouch:Touch
	agony_internal var m_bindingHoverNode:PixelsAA
	
	
	
	[Inline]
	final override agony_internal function dispose() : void {
		this.removeAllListeners()
		m_combine.recycle()
	}
	
	[Inline]
	final override agony_internal function doUpdateDownward() : void {
		var N_A:Number, N_B:Number
		
		N_A  =  Math.cos(m_rotation * Math.PI / 180)
		N_B  =  Math.sin(m_rotation * Math.PI / 180)
		m_matrix_B.a  =  m_matrix_A.a  =   m_scaleX * N_A
		m_matrix_B.b  =  m_matrix_A.b  =   m_scaleY * N_B
		m_matrix_B.c  =  m_matrix_A.c  =  -m_scaleX * N_B
		m_matrix_B.d  =  m_matrix_A.d  =   m_scaleY * N_A
		if (m_pivotX != 0.0 || m_pivotY != 0.0 || m_renderTex.m_frame) {
			N_A = m_renderTex.m_frame ? m_pivotX + m_renderTex.m_frame.x : m_pivotX
			N_B = m_renderTex.m_frame ? m_pivotY + m_renderTex.m_frame.y : m_pivotY
			m_matrix_B.tx  =  m_matrix_A.tx  =  m_x - m_matrix_B.a * N_A - m_matrix_B.c * N_B
			m_matrix_B.ty  =  m_matrix_A.ty  =  m_y - m_matrix_B.b * N_A - m_matrix_B.d * N_B
		}
		else {
			m_matrix_B.tx  =  m_matrix_A.tx  =  m_x
			m_matrix_B.ty  =  m_matrix_A.ty  =  m_y
		}
	}
	
	[Inline]
	final override agony_internal function updateByAlone() : void {
		var N_A:Number, N_B:Number
		
		N_A  =  Math.cos(m_rotation * Math.PI / 180)
		N_B  =  Math.sin(m_rotation * Math.PI / 180)
		m_matrix_A.a  =   m_scaleX * N_A
		m_matrix_A.b  =   m_scaleY * N_B
		m_matrix_A.c  =  -m_scaleX * N_B
		m_matrix_A.d  =   m_scaleY * N_A
		if (m_pivotX != 0.0 || m_pivotY != 0.0 || (m_renderTex && m_renderTex.m_frame)) {
			N_A = (m_renderTex && m_renderTex.m_frame) ? m_pivotX + m_renderTex.m_frame.x : m_pivotX
			N_B = (m_renderTex && m_renderTex.m_frame) ? m_pivotY + m_renderTex.m_frame.y : m_pivotY
			m_matrix_A.tx  =  m_x - m_matrix_B.a * N_A - m_matrix_B.c * N_B
			m_matrix_A.ty  =  m_y - m_matrix_B.b * N_A - m_matrix_B.d * N_B
		}
		else {
			m_matrix_A.tx  =  m_x
			m_matrix_A.ty  =  m_y
		}
	}
	
	[Inline]
	final override agony_internal function checkCollisionNode( globalX:Number, globalY:Number, forInteract:Boolean ) : NodeAA {
		if ((forInteract && m_intouchable) || m_invisible) {
			return null
		}
		this.doTransformToLocal(globalX, globalY)
		if (m_collisionMethod != null) {
			return m_collisionMethod(cachedPoint.x, cachedPoint.y, this) ? this : null
		}
		cachedRect.setTo(0, 0, m_renderTex.m_frame ? m_renderTex.m_frame.width : m_renderTex.m_realityWidth, m_renderTex.m_frame ? m_renderTex.m_frame.height : m_renderTex.m_realityHeight)
		return cachedRect.containsPoint(cachedPoint) ? this : null
	}
	
	[Inline]
	final override agony_internal function doTransformToLocal( x:Number, y:Number ) : void {
		cachedMatrix.copyFrom(m_matrix_B)
		cachedMatrix.invert()
		if (m_renderTex.m_frame) {
			cachedPoint.setTo(cachedMatrix.a * x + cachedMatrix.c * y + cachedMatrix.tx - m_renderTex.m_frame.x, cachedMatrix.d * y + cachedMatrix.b * x + cachedMatrix.ty - m_renderTex.m_frame.y)
		}
		else {
			cachedPoint.setTo(cachedMatrix.a * x + cachedMatrix.c * y + cachedMatrix.tx, cachedMatrix.d * y + cachedMatrix.b * x + cachedMatrix.ty)
		}
	}
	
	[Inline]
	final agony_internal function bubble( type:String, toWorld:Boolean ) : void {
		var node:NodeAA
		
		node = this
		node.dispatchDirectEvent(type)
		node = node.m_parent
		while (node && (toWorld || !(node is WorldAA))) {
			node.dispatchDirectEvent(type)
			node = node.m_parent
		}
	}
	
	/** overwrite */
	agony_internal function doPushToConvergence( vertexChanged:Boolean ) : void {
		
	}
}
}